package com.ruoyi.wx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wx.domain.SharedTenant;
import com.ruoyi.wx.service.ISharedTenantService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合租人Controller
 * 
 * @author wt
 * @date 2020-12-13
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wx/sharedTenant" )
public class SharedTenantController extends BaseController {

    private final ISharedTenantService iSharedTenantService;

    /**
     * 查询合租人列表
     */
    @PreAuthorize("@ss.hasPermi('wx:sharedTenant:list')")
    @GetMapping("/list")
    public TableDataInfo list(SharedTenant sharedTenant)
    {
        startPage();
        LambdaQueryWrapper<SharedTenant> lqw = Wrappers.lambdaQuery(sharedTenant);
        if (StringUtils.isNotBlank(sharedTenant.getName())){
            lqw.like(SharedTenant::getName ,sharedTenant.getName());
        }
        if (StringUtils.isNotBlank(sharedTenant.getMemo())){
            lqw.eq(SharedTenant::getMemo ,sharedTenant.getMemo());
        }
        if (sharedTenant.getSettlementOfTheMonth() != null){
            lqw.eq(SharedTenant::getSettlementOfTheMonth ,sharedTenant.getSettlementOfTheMonth());
        }
        if (sharedTenant.getDividendRatio() != null){
            lqw.eq(SharedTenant::getDividendRatio ,sharedTenant.getDividendRatio());
        }
        if (sharedTenant.getState() != null){
            lqw.eq(SharedTenant::getState ,sharedTenant.getState());
        }
        if (sharedTenant.getDividendStatus() != null){
            lqw.eq(SharedTenant::getDividendStatus ,sharedTenant.getDividendStatus());
        }
        List<SharedTenant> list = iSharedTenantService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出合租人列表
     */
    @PreAuthorize("@ss.hasPermi('wx:sharedTenant:export')" )
    @Log(title = "合租人" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(SharedTenant sharedTenant) {
        LambdaQueryWrapper<SharedTenant> lqw = new LambdaQueryWrapper<SharedTenant>(sharedTenant);
        List<SharedTenant> list = iSharedTenantService.list(lqw);
        ExcelUtil<SharedTenant> util = new ExcelUtil<SharedTenant>(SharedTenant. class);
        return util.exportExcel(list, "sharedTenant" );
    }

    /**
     * 获取合租人详细信息
     */
    @PreAuthorize("@ss.hasPermi('wx:sharedTenant:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iSharedTenantService.getById(id));
    }

    /**
     * 新增合租人
     */
    @PreAuthorize("@ss.hasPermi('wx:sharedTenant:add')" )
    @Log(title = "合租人" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SharedTenant sharedTenant) {
        return toAjax(iSharedTenantService.save(sharedTenant) ? 1 : 0);
    }

    /**
     * 修改合租人
     */
    @PreAuthorize("@ss.hasPermi('wx:sharedTenant:edit')" )
    @Log(title = "合租人" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SharedTenant sharedTenant) {
        return toAjax(iSharedTenantService.updateById(sharedTenant) ? 1 : 0);
    }

    /**
     * 删除合租人
     */
    @PreAuthorize("@ss.hasPermi('wx:sharedTenant:remove')" )
    @Log(title = "合租人" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iSharedTenantService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
